/*

 */
package com.ohos.connect.devicecloud;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.ohos.connect.devicecloud.mqtt.core.model.dto.CoapToHttpDTO;
import com.ohos.connect.devicecloud.mqtt.modular.coap.service.CoapService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;

/**
 * 主测试类
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MainTest {
    @Autowired
    CoapService coapService;

    /**
     * 测试发送coap消息
     */
    @Test
    public void test() {
        CoapToHttpDTO coapToHttpDTO = new CoapToHttpDTO();
        coapToHttpDTO.setMethod("POST");
        coapToHttpDTO.setUrl("/.sys/switch");
        HashMap<String, Object> service = new HashMap<>();
        service.put("devId", "41a5cd8c-7b17-44be-9f30-afdce7420c76");
        service.put("sn1", "b4582f5e2a55c193");
        service.put("seq", 201);
        coapToHttpDTO.setBody(JSON.toJSONString(service));
        HashMap<String, Object> option = new HashMap<>();
        option.put("REQ_ID", "237c485fb7ce2599109895642c169e459e2d7e8a");
        option.put("DEV_ID", "41a5cd8c-7b17-44be-9f30-afdce7420c76");
        option.put("SEQ_NUM_ID", "1350066738");
        option.put("ACCESS_TOKEN_ID", "123456");
        coapToHttpDTO.setOption(option);

        coapService.httpToCoapMethod(coapToHttpDTO);


    }


    @Test
    public void sendTCP() {

        
    }


    public static void main(String[] args) {
        System.out.println(getRandomSn().length());
        System.out.println(getRandomSn());
    }


    private static String getRandomSn() {
        // 生成8字节的随机数据
        byte[] randomBytes = RandomUtil.randomBytes(8);

        // 将字节数组转换成十六进制字符串
        // Hutool没有直接提供字节到十六进制字符串的转换方法，所以我们使用Java标准库
        StringBuilder hexString = new StringBuilder(2 * randomBytes.length); // 每个字节变成两个十六进制字符
        for (byte b : randomBytes) {
            // 将每个字节转换成两位的十六进制数并追加到StringBuilder中
            hexString.append(String.format("%02x", b & 0xff));
        }

        return hexString.toString();
    }


}
